﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Library
{
	class Camera2D
	{
		/// <summary>
		/// Borrowed from http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/
		/// </summary>


		protected static float _zoom; // Camera Zoom
		public static  Matrix _transform; // Matrix Transform
		public static Vector2 _pos; // Camera Position
		protected static float _rotation; // Camera Rotation

		public Camera2D()
		{
			_zoom = 1.0f;
			_rotation = 0.0f;
			_pos = Vector2.Zero;
		}


		// Sets and gets zoom
		public float Zoom
		{
			get { return _zoom; }
			set { _zoom = value; if (_zoom < 0.1f) _zoom = 0.1f; } // Negative zoom will flip image
		}

		public float Rotation
		{
			get { return _rotation; }
			set { _rotation = value; }
		}

		// Auxiliary function to move the camera
		public static void Move(Vector2 amount)
		{
			_pos += amount;
		}
		public static void Follow(Vector2 position)
		{
			if (_pos != position - GameSettings.GetResolution() / 2)
			{
				_pos = position - GameSettings.GetResolution() / 2;
			}
		}
		public static Rectangle GetScreenRectangle()
		{
			return new Rectangle((int)_pos.X, (int)_pos.Y, (int)GameSettings.GetResolution().X, (int)GameSettings.GetResolution().Y);
		}
		// Get set position
		public Vector2 Pos
		{
			get { return _pos; }
			set { _pos = value; }
		}
		public Matrix GetTransformation(GraphicsDevice graphicsDevice)
		{
			_transform =       // Thanks to o KB o for this solution
			  Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
										 Matrix.CreateRotationZ(Rotation) *
										 Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
										 Matrix.CreateTranslation(new Vector3(0,0, 0));
			return _transform;
		}
	}
}
